define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'check/after_sale/index',
                    add_url: 'check/after_sale/add',
                    edit_url: 'check/after_sale/edit',
                    del_url: 'check/after_sale/del',
                    multi_url: 'check/after_sale/multi',
                    table: 'after_sale',
                }
            });

            var table = $("#table");
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-audit").data("area", ["90%", "90%"]);
                $(".btn-detail").data("area", ["90%", "90%"]);
                $(".btn-assign").data("area", ["90%", "90%"]);
                $(".btn-d4").data("area", ["80%", "80%"]);
                $(".btn-d5").data("area", ["80%", "80%"]);
            });
            var check_id = [];
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                maintainSelected: 1,
                showSearch: false,
                search: false,
                onCheck: function (row, tr, flied) {
                    check_id.push(row.id)
                    //  console.log(row);
                },
                columns: [
                    [
                        {
                            checkbox: true, formatter: function (value, row, index) {
                                //                                        if (row.check == 1) {
                                //                                            return { checked: true };
                                //                                        }

                                if (check_id.includes(row.id)) {
                                    return {checked: true};
                                }
                            }
                        },
                        {field: 'lift_number', title: __('电梯编号'), operate: 'like'},
                        {field: 'order_id', title: __('订单编号'), operate: 'like'},
                        {field: 'specifications', title: __('电梯规格'), operate: false},
                        {
                            field: 'specifications_name',  title: '电梯规格', searchList: function (column) {
                                return Template('specifications_name', {});
                            }, formatter: function (value, row, index) {
                                return '无';
                            }, visible: false
                        },
                        {field: 'l_real_name', title: '业主姓名', operate: 'like'},
                        {field: 'phone', title: '联系电话', operate: 'like'},
                        {field: 'area', title: '所在地区', operate: false},
                        {
                            field: 'address1',  title: '所在地区', searchList: function (column) {
                                return Template('address1', {});
                            }, formatter: function (value, row, index) {
                                return '无';
                            }, visible: false
                        },
                        {field: 'specific_address', title: '电梯地址', operate: 'like'},
                        {field: 'pay_price', title: '成交价格（¥）', operate: false},
                        {field: 'package', title: '套餐名称', operate: false},
                        {
                            field: 'package_name',  title: '套餐名称', searchList: function (column) {
                                return Template('package_name', {});
                            }, formatter: function (value, row, index) {
                                return '无';
                            }, visible: false
                        },
                        {field: 'duration', title: '套餐时长', operate: false},
                        {
                            field: 'duration_name',  title: '套餐时长', searchList: function (column) {
                                return Template('duration_name', {});
                            }, formatter: function (value, row, index) {
                                return '无';
                            }, visible: false
                        },
                        {field: 'id', title: '售后编号', operate: 'like'},
                        {
                            field: 'audit_status', title: __('售后状态'),
                            searchList: {
                                //                                "1": __('待助力'),
                                "0": __('待审核'),
                                "1": __('已退款'),
                                "2": __('已关闭'),
                            },
                            formatter: function (value, row) {
                                if (value == 1) {
                                    return '<span style="">已退款</span>';
                                } else if (value == 2) {
                                    return '<span style="">已关闭</span>';
                                } else if (value == 0) {
                                    return '待审核';
                                }
                            }
                        },
                        {
                            field: 'refund_reason', title: __('售后原因'),
                            searchList: {
                                //                                "1": __('待助力'),
                                "1": __('非加装电梯首次服务不满意退款'),
                                "2": __('7天内未使用申请退款'),
                                "3": __('其它'),
                            },
                            formatter: function (value, row) {
                                if (value == 1) {
                                    return '<span style="">非加装电梯首次服务不满意退款</span>';
                                } else if (value == 2) {
                                    return '<span style="">7天内未使用申请退款</span>';
                                } else if (value == 3) {
                                    return '其它';
                                }
                            }
                        },
//                        { field: 'elevator_no', title: '电梯编号', operate: 'like' },
//                        {
//                            field: 'clean_area', title: __('清洁范围'),
//                            searchList: {
//                                //                                "1": __('待助力'),
//                                "1": __('轿厢内部四个面'),
//                                "2": __('轿厢内部四个面+地板+厅门'),
//                            },
//                            formatter: function (value) {
//                                switch (value) {
//                                    case 1:
//                                        value = '轿厢内部四个面';
//                                        break;
//                                    case 2:
//                                        value = '轿厢内部四个面+地板+厅门';
//                                        break;
//
//                                }
//                                return value;
//                            }
//                        },
                        {
                            field: 'add_time',
                            title: __('申请时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true
                        },
                        {field: 'u_real_name', title: '业务员', operate: false},
                        {
                            field: 'personnel_id', title: '业务员', addclass: 'selectpage',
                            extend: 'data-source="check/free_clean/getSaleMan" data-field="title"', visible: false
                        },
                        {
                            field: 'operate', title: __('Operate'), table: table, buttons: [
                                {
                                    name: 'detail',
                                    text: __('查看详情'),
                                    title: __('查看详情'),
                                    classname: 'btn btn-xs btn-success btn-detail btn-magic btn-dialog',
                                    icon: 'fa fa-eye',
                                    url: 'check/after_sale/detail?id={id}',
                                    // extend:'data-area=["60%","70%"]',
                                    callback: function (data) {
                                    },
                                },
                                {
                                    name: 'audit',
                                    text: __('审核'),
                                    title: __('审核'),
                                    classname: 'btn btn-xs btn-warning btn-assign btn-magic btn-dialog',
                                    icon: 'fa fa-paint-brush',
                                    url: 'check/after_sale/audit?id={id}',
                                    // extend:'data-area=["60%","70%"]',
                                    callback: function (data) {
                                    },
                                },
                            ], events: Table.api.events.operate, formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                if (row.status == 3) {
                                    $(table).data("operate-audit", false); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                } else {
                                    $(table).data("operate-audit", true); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                }
                                $(table).data("operate-edit", null); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                $(table).data("operate-del", null); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        },
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
            $(document).on("click", ".btn-most-assign", function () {
                var id = $(this).attr('val');
                console.log(check_id.join(","));
                //var nick_name=$(this).attr('nickname');
                Fast.api.open('check/free_clean/do_most_assign?check_id=' + check_id.join(",") + '', __('批量派送'), {area: ['50%', '50%']}, {
                    callback: function (value) {

                    }
                });
            });
        },
        after_sale: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'check/after_sale/after_sale',
                    add_url: 'check/after_sale/add',
                    edit_url: 'check/after_sale/edit',
                    del_url: 'check/after_sale/del',
                    multi_url: 'check/after_sale/multi',
                    table: 'after_sale',
                }
            });

            var table = $("#table");
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-audit").data("area", ["90%", "90%"]);
                $(".btn-detail").data("area", ["90%", "90%"]);
                $(".btn-aduit").data("area", ["90%", "90%"]);
                $(".btn-assign").data("area", ["50%", "50%"]);
                $(".btn-d4").data("area", ["80%", "80%"]);
                $(".btn-d5").data("area", ["80%", "80%"]);
            });
            var check_id = [];
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                showSearch: false,
                search: false,
                maintainSelected: 1,
                onCheck: function (row, tr, flied) {
                    check_id.push(row.id)
                    //  console.log(row);
                },
                columns: [
                    [
                        {
                            checkbox: true, formatter: function (value, row, index) {
                                //                                        if (row.check == 1) {
                                //                                            return { checked: true };
                                //                                        }

                                if (check_id.includes(row.id)) {
                                    return {checked: true};
                                }
                            }
                        },
                        {field: 'number', title: __('售后编号'), operate: 'like'},
                        {field: 'l_real_name', title: '业主姓名', operate: 'like'},
                        {field: 'phone', title: '联系电话', operate: 'like'},
                        {field: 'package', title: '套餐名称', operate: false},
                        {
                            field: 'package_name',  title: '套餐名称', searchList: function (column) {
                                return Template('package_name', {});
                            }, formatter: function (value, row, index) {
                                return '无';
                            }, visible: false
                        },
                        {field: 'duration', title: '套餐时长', operate: false},
                        {
                            field: 'duration_name',  title: '套餐时长', searchList: function (column) {
                                return Template('duration_name', {});
                            }, formatter: function (value, row, index) {
                                return '无';
                            }, visible: false
                        },
                        {field: 'specifications', title: __('电梯规格'), operate: false},
                        {
                            field: 'specifications_name',  title: '电梯规格', searchList: function (column) {
                                return Template('specifications_name', {});
                            }, formatter: function (value, row, index) {
                                return '无';
                            }, visible: false
                        },
                        {field: 'service_time', title: __('服务日期'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true},
                        {
                            field: 'add_time',
                            title: __('申请时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true
                        },
                        {field: 'lift_number', title: __('电梯编号'), operate: 'like'},

                        {field: 'area', title: '所在地区', operate: false},
                        {
                            field: 'address1',  title: '所在地区', searchList: function (column) {
                                return Template('address1', {});
                            }, formatter: function (value, row, index) {
                                return '无';
                            }, visible: false
                        },
                        {field: 'specific_address', title: '电梯地址', operate: 'like'},
                        {
                            field: 'service_status', title: __('售后状态'),
                            searchList: {
                                //                                "1": __('待助力'),
                                "1": __('已处理'),
                                "2": __('未处理'),
                                "3": __('已关闭'),
                            },
                            formatter: function (value, row) {
                                if (value == 1) {
                                    return '<span style="">已处理</span>';
                                } else if (value == 2) {
                                    return '<span style="">未处理</span>';
                                } else if (value == 3) {
                                    return '已关闭';
                                }
                            }
                        },
                        {
                            field: 'remark', title: __('售后原因'),
                            searchList: {
                                //                                "1": __('待助力'),
                                "1": __('电梯损坏'),
                                "2": __('保洁员态度恶劣'),
                                "3": __('其它'),
                            },
                            formatter: function (value, row) {
                                if (value == 1) {
                                    return '<span style="">电梯损坏</span>';
                                } else if (value == 2) {
                                    return '<span style="">保洁员态度恶劣</span>';
                                } else if (value == 3) {
                                    return '其它';
                                }
                            }
                        },
//                        { field: 'elevator_no', title: '电梯编号', operate: 'like' },
//                        {
//                            field: 'clean_area', title: __('清洁范围'),
//                            searchList: {
//                                //                                "1": __('待助力'),
//                                "1": __('轿厢内部四个面'),
//                                "2": __('轿厢内部四个面+地板+厅门'),
//                            },
//                            formatter: function (value) {
//                                switch (value) {
//                                    case 1:
//                                        value = '轿厢内部四个面';
//                                        break;
//                                    case 2:
//                                        value = '轿厢内部四个面+地板+厅门';
//                                        break;
//
//                                }
//                                return value;
//                            }
//                        },

                        {field: 'user.real_name', title: '业务员', operate: false},
                        {
                            field: 'personnel_id', title: '业务员', addclass: 'selectpage',
                            extend: 'data-source="check/free_clean/getSaleMan" data-field="title"', visible: false
                        },
                        {
                            field: 'operate', title: __('Operate'), table: table, buttons: [
                                {
                                    name: 'detail',
                                    text: __('查看详情'),
                                    title: __('查看详情'),
                                    classname: 'btn btn-xs btn-success btn-detail btn-magic btn-dialog',
                                    icon: 'fa fa-eye',
                                    url: 'check/after_sale/after_sale_detail?id={id}',
                                    // extend:'data-area=["60%","70%"]',
                                    callback: function (data) {
                                    },
                                },
                                {
                                    name: 'aduit',
                                    text: __('审核'),
                                    title: __('审核'),
                                    classname: 'btn btn-xs btn-warning btn-aduit btn-magic btn-dialog',
                                    icon: 'fa fa-paint-brush',
                                    url: 'check/after_sale/service_audit?id={id}',
                                    // extend:'data-area=["60%","70%"]',
                                    callback: function (data) {
                                    },
                                },
                            ], events: Table.api.events.operate, formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                if (row.status == 2) {
                                    $(table).data("operate-audit", true); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                } else {
                                    $(table).data("operate-audit", false); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                }
                                $(table).data("operate-edit", null); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                $(table).data("operate-del", null); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        },
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
            $(document).on("click", ".btn-most-assign", function () {
                var id = $(this).attr('val');
                console.log(check_id.join(","));
                //var nick_name=$(this).attr('nickname');
                Fast.api.open('check/free_clean/do_most_assign?check_id=' + check_id.join(",") + '', __('批量派送'), {area: ['50%', '50%']}, {
                    callback: function (value) {

                    }
                });
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        do_most_assign: function () {
            Controller.api.bindevent();
        },
        //审核
        audit: function () {
            Controller.api.bindevent();
            let audit_status1 = document.getElementById('row[audit_status]-1')
            let audit_status2 = document.getElementById('row[audit_status]-2')
            //监听是开票的情况
            audit_status1.addEventListener('click', function () {
                $('.bh').hide();
            });

            //监听不是开票的情况
            audit_status2.addEventListener('click', function () {
                $('.bh').show();
            });
        },
        //审核
        service_audit: function () {
            Controller.api.bindevent();
            let service_status1 = document.getElementById('row[service_status]-1')
            let service_status3 = document.getElementById('row[service_status]-3')
            //监听是开票的情况
            service_status1.addEventListener('click', function () {
                $('.bh').hide();
            });

            //监听不是开票的情况
            service_status3.addEventListener('click', function () {
                $('.bh').show();
            });
        },
        //指派业务员
        do_assign: function () {
            Controller.api.bindevent();
            var is_have_saleman = Config.is_have_saleman;
            if (is_have_saleman == 1) {
                $('.saleman').show();
            } else {
                $('.saleman').hide();
            }
        },

        service_after_sale: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'check/after_sale/service_after_sale',
                    add_url: 'check/after_sale/add',
                    edit_url: 'check/after_sale/edit',
                    del_url: 'check/after_sale/del',
                    multi_url: 'check/after_sale/multi',
                    table: 'after_sale',
                }
            });

            var table = $("#table");
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-audit").data("area", ["90%", "90%"]);
                $(".btn-detail").data("area", ["90%", "90%"]);
                $(".btn-detail").data("area", ["90%", "90%"]);
                $(".btn-assign").data("area", ["50%", "50%"]);
                $(".btn-d4").data("area", ["80%", "80%"]);
                $(".btn-d5").data("area", ["80%", "80%"]);
            });
            var check_id = [];
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                maintainSelected: 1,
                onCheck: function (row, tr, flied) {
                    check_id.push(row.id)
                    //  console.log(row);
                },
                columns: [
                    [
                        {
                            checkbox: true, formatter: function (value, row, index) {
                                //                                        if (row.check == 1) {
                                //                                            return { checked: true };
                                //                                        }

                                if (check_id.includes(row.id)) {
                                    return {checked: true};
                                }
                            }
                        },
                        {field: 'id', title: __('电梯编号'), sortable: true},
                        {field: 'id', title: __('电梯规格'), sortable: true},
                        {field: 'proprietor_name', title: '业主姓名', operate: 'like'},
                        {field: 'phone', title: '联系电话', operate: 'like'},
                        {field: 'address', title: '所在地区', operate: 'like'},
                        {field: 'buildings', title: '电梯地址', operate: 'like'},
                        {field: 'buildings', title: '成交价格(￥)', operate: false},
                        {field: 'buildings', title: '套餐名称', operate: false},
                        {field: 'buildings', title: '套餐时长', operate: false},
                        {field: 'buildings', title: '套餐时长', operate: false},

//                        { field: 'elevator_no', title: '电梯编号', operate: 'like' },
//                        {
//                            field: 'clean_area', title: __('清洁范围'),
//                            searchList: {
//                                //                                "1": __('待助力'),
//                                "1": __('轿厢内部四个面'),
//                                "2": __('轿厢内部四个面+地板+厅门'),
//                            },
//                            formatter: function (value) {
//                                switch (value) {
//                                    case 1:
//                                        value = '轿厢内部四个面';
//                                        break;
//                                    case 2:
//                                        value = '轿厢内部四个面+地板+厅门';
//                                        break;
//
//                                }
//                                return value;
//                            }
//                        },
                        {
                            field: 'add_time',
                            title: __('创建时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true
                        },
                        {
                            field: 'add_time',
                            title: __('清洁结束时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true
                        },
                        {
                            field: 'status', title: __('订单状态'),
                            searchList: {
                                //                                "1": __('待助力'),
                                "12": __('服务中'),
                                "13": __('服务完成'),
                                "14": __('已取消'),
                            },
                            formatter: function (value, row) {
                                if (value == 12) {
                                    return '<span style="">服务中</span>';
                                } else if (value == 13) {
                                    return '<span style="">服务完成</span>';
                                } else if (value == 14) {
                                    return '已取消';
                                }
                            }
                        },
                        {field: 'saleman.real_name', title: '业务员',},
                        {
                            field: 'operate', title: __('Operate'), table: table, buttons: [
                                {
                                    name: 'detail',
                                    text: __('查看详情'),
                                    title: __('查看详情'),
                                    classname: 'btn btn-xs btn-success btn-detail btn-magic btn-dialog',
                                    icon: 'fa fa-eye',
                                    url: 'check/order/detail?id={id}',
                                    // extend:'data-area=["60%","70%"]',
                                    callback: function (data) {
                                    },
                                },
                                {
                                    name: 'postpone',
                                    text: __('申请延期'),
                                    title: __('申请延期'),
                                    classname: 'btn btn-xs btn-warning btn-audit btn-magic btn-dialog',
                                    icon: 'fa fa-paint-brush',
                                    url: 'check/order/postpone?id={id}',
                                    // extend:'data-area=["60%","70%"]',
                                    callback: function (data) {
                                    },
                                },
                                {
                                    name: 'assign',
                                    text: __('指派业务员'),
                                    title: __('指派业务员'),
                                    classname: 'btn btn-xs btn-danger btn-assign btn-magic btn-dialog',
                                    icon: 'fa fa-paint-brush',
                                    url: 'check/order/do_assign?id={id}',
                                    // extend:'data-area=["60%","70%"]',
                                    callback: function (data) {
                                    },
                                },
                                {
                                    name: 'see_service_order',
                                    text: __('查看服务单'),
                                    title: __('查看服务单'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-addtabs',
                                    icon: 'fa fa-eye',
                                    url: 'check/order/see_service_order?id={id}',
                                    // extend:'data-area=["60%","70%"]',
                                    callback: function (data) {
                                    },
                                },
                                {
                                    name: 'after_sale',
                                    text: __('申请售后'),
                                    title: __('申请售后'),
                                    classname: 'btn btn-xs btn-danger btn-assign btn-magic btn-dialog',
                                    icon: 'fa fa-paint-brush',
                                    url: 'check/order/after_sale?id={id}',
                                    // extend:'data-area=["60%","70%"]',
                                    callback: function (data) {
                                    },
                                },
                            ], events: Table.api.events.operate, formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                if (row.status == 3) {
                                    $(table).data("operate-audit", false); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                } else {
                                    $(table).data("operate-audit", true); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                }
                                $(table).data("operate-edit", true); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                $(table).data("operate-del", null); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        },
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
            $(document).on("click", ".btn-most-assign", function () {
                var id = $(this).attr('val');
                console.log(check_id.join(","));
                //var nick_name=$(this).attr('nickname');
                Fast.api.open('check/free_clean/do_most_assign?check_id=' + check_id.join(",") + '', __('批量派送'), {area: ['50%', '50%']}, {
                    callback: function (value) {

                    }
                });
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
